package com.ryan.mms.privilage.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ryan.framework.dao.impl.BaseHibernateDaoSupport;
import com.ryan.framework.tag.pagination.Page;
import com.ryan.mms.privilage.dao.EmployeeDao;
import com.ryan.mms.privilage.model.Employee;
import com.ryan.utils.StringUtil;

@Repository
public class EmployeeDaoImpl extends BaseHibernateDaoSupport<Employee, Long> implements EmployeeDao<Employee,Long> {
	private final String deleteEmployeeSql = "delete from Employee employee where employee.employeeId in (?)";


	public List<Employee> findByEmployee(Employee employee) {
		List<Employee> employeeList = this.find(generateCriterionList(employee));
		return employeeList;
	}

	public List<Employee> findByEmployee(Page page, Employee employee) {
		List<Employee> employeeList = this.find(page,generateCriterionList(employee));
		return employeeList;
	}
	
	private List<Criterion> generateCriterionList(Employee employee){
		List<Criterion> criterionList=new ArrayList<Criterion>();
		if(!StringUtil.isEmpty(employee.getEmployeeCode())){
			criterionList.add(Restrictions.like("employeeCode", employee.getEmployeeCode()));
		}
		if(!StringUtil.isEmpty(employee.getEmployeeName())){
			criterionList.add(Restrictions.like("employeeName", employee.getEmployeeName()));
		}
		if(!StringUtil.isEmpty(employee.getIdCard())){
			criterionList.add(Restrictions.like("idCard", employee.getIdCard()));
		}
		if(!StringUtil.isNull(employee.getOrganization())&&null!=employee.getOrganization().getOrganizationId()&&!new Long(-1).equals(employee.getOrganization().getOrganizationId())){
			criterionList.add(Restrictions.eq("organization", employee.getOrganization()));
		}
		if(null!=employee.getPositions()&&!employee.getPositions().isEmpty()){
			criterionList.add(Restrictions.in("positions", employee.getPositions()));
		}
		if(!StringUtil.isNull(employee.getGender())){
			criterionList.add(Restrictions.eq("gender", employee.getGender()));
		}
		if(!StringUtil.isNull(employee.getEducation())){
			criterionList.add(Restrictions.eq("education", employee.getEducation()));
		}
		return criterionList;
	}

	public void deleteByEmployeeIds(List<Long> pks) {
		for(String pksStr :getPksList(pks)){
			super.execute(deleteEmployeeSql, pksStr);
		}
	}

	public void deleteByEmployeeIds(String pks) {
		super.execute(deleteEmployeeSql, pks);
	}
	
	public List<String> getPksList(List<Long> pks){
		List<String> idsList = new ArrayList<String>();
		StringBuilder pksStrBuilder = new StringBuilder();
		int i=0;
		for(Long pk:pks){
			pksStrBuilder.append(",").append(pk);
			if(i==998){
				String pksStr=pksStrBuilder.toString();
				pksStr = pksStr.substring(1);
				idsList.add(pksStr);
				pksStrBuilder = new StringBuilder();
				i=-1;
			}
		}
		if(i==998){
			String pksStr = pksStrBuilder.toString();
			if(null!=pksStr&&!"".equals(pksStr)){
				pksStr = pksStr.substring(1);
			}
		}
		return idsList;
	}
}
